package com.youbug.mall.order_oms.feign.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youbug.common.constant.AuthServerConstant;
import com.youbug.common.to.OrderTo;
import com.youbug.common.to.UmsMemberTo;
import com.youbug.common.vo.QueryVo;
import com.youbug.common.vo.R;
import com.youbug.mall.member_ums.feign.IMemberOrderFeignService;
import com.youbug.mall.order_oms.entity.OmsOrder;
import com.youbug.mall.order_oms.service.IOmsOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MemberOrderFeignServiceImpl implements IMemberOrderFeignService {
    @Autowired
    private IOmsOrderService orderService;

    @Autowired
    private HttpServletRequest request;

    @Override
    public R<IPage<OrderTo>> getOrderWithItem() {

        HttpSession session = request.getSession();
        //获得当前登录用户的信息
        UmsMemberTo memberTo = (UmsMemberTo) session.getAttribute(AuthServerConstant.LOGIN_USER);

        QueryVo<OmsOrder> queryVo = new QueryVo<>();
        OmsOrder order = new OmsOrder();
        order.setMemberId(memberTo.getId());
        queryVo.setPayload(order);

        IPage<OmsOrder> iPage = orderService.getOmsOrderPageWithItem(queryVo);

        IPage<OrderTo> page = new Page<OrderTo>();

        List<OrderTo> tos = iPage.getRecords().stream().map(e -> {
            OrderTo to = new OrderTo();
            BeanUtils.copyProperties(e, to);
            return to;
        }).collect(Collectors.toList());

        page.setRecords(tos);

        return R.ok(page);
    }
}
